package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation(value = "新增菜品分类")
    public Result save(@RequestBody CategoryDTO dto) {
        log.info("新增菜品分类：{}", dto);
        categoryService.save(dto);
        return Result.success();
    }


    @ApiOperation(value = "分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO dto) {
        log.info("分类分页查询：{}", dto);
        PageResult page = categoryService.page(dto);
        return Result.success(page);
    }


    @ApiOperation(value = "删除菜品分类")
    @DeleteMapping
    public Result deleteById(@RequestParam(value = "id", required = true) Long id) {
        log.info("删除菜品分类:{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }


    @ApiOperation(value = "分类启用禁用")
    @PostMapping("/status/{status}")
    public Result status(
            @PathVariable Integer status,
            @RequestParam(value = "id", required = true) Long id) {
        log.info("分类启用禁用:status={},id={}", status, id);
        categoryService.status(status, id);
        return Result.success();
    }


    @ApiOperation(value = "修改分类")
    @PutMapping
    public Result update(@RequestBody CategoryDTO dto) {
        log.info("修改分类：dto={}", dto);
        categoryService.update(dto);
        return Result.success();
    }


    @ApiOperation(value = "根据类型查询")
    @GetMapping("/list")
    public Result<List<Category>> list(@RequestParam(value = "type", defaultValue = "1") Integer type) {
        log.info("根据类型查询：type={}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

}
